package LeetCode_1_100;

class Solution07 {
    public int reverse(int x) {
        int reversed = 0;
        while (x != 0) {
            int digit = x % 10;
            x /= 10;

            // 检查正数溢出
            if (reversed > Integer.MAX_VALUE / 10 || (reversed == Integer.MAX_VALUE / 10 && digit > 7)) {
                return 0;
            }
            // 检查负数溢出
            if (reversed < Integer.MIN_VALUE / 10 || (reversed == Integer.MIN_VALUE / 10 && digit < -8)) {
                return 0;
            }

            reversed = reversed * 10 + digit;
        }
        return reversed;
    }
}

public class LeetCode07_reverse {
    public static void main(String[] args) {
        Solution07 solution = new Solution07();
        int x;

        //Case 1
        x=123;
        System.out.println(solution.reverse(x));

        //Case 2
        x=-123;
        System.out.println(solution.reverse(x));

        //Case 3
        x=120;
        System.out.println(solution.reverse(x));

    }
}
